package controller

import (
	bc "Agricultural/application/blockchain"
	"Agricultural/application/lib"
	"Agricultural/application/util"
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

// 创建测试肥力
func CreateTestFertility(ctx *gin.Context)  {
	// 解析结构体
	req := new(lib.TestFertility)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	req.Id = util.Krand(11, 0)
	req.CreateDate = time.Now()
	marshal, _ := json.Marshal(req)
	resp, err := bc.ChannelExecute("createTestFertility", [][]byte{
		marshal,
		[]byte(req.Space),
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

// 为场地施肥
func CreateAddFertility(ctx *gin.Context)  {
	// 解析结构体
	req := new(lib.AddFertility)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	req.Id = util.Krand(11, 0)
	req.CreateDate = time.Now()
	marshal, _ := json.Marshal(req)
	resp, err := bc.ChannelExecute("createAddFertility", [][]byte{
		marshal,
		[]byte(req.Space),
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

// 创建一个农作物对象
func CreateCrops(ctx *gin.Context)  {
	subId := ctx.Query("subId")
	// 解析结构体
	req := new(lib.Crops)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	marshal, _ := json.Marshal(req)
	resp, err := bc.ChannelExecute("createCrops", [][]byte{
		marshal,
		[]byte(subId),
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

// 更新农作物状态
func UpdateCrops(ctx *gin.Context)  {
	cropsId := ctx.Query("cropsId")
	cropsStatus := ctx.Query("cropsStatus")
	resp, err := bc.ChannelExecute("updateCrops", [][]byte{
		[]byte(cropsId),
		[]byte(cropsStatus),
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

// 采摘农作物
func PickCrops(ctx *gin.Context)  {
	subId := ctx.Query("subId")
	// 解析结构体
	req := new(lib.Pick)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	marshal, _ := json.Marshal(req)
	resp, err := bc.ChannelExecute("pickCrops", [][]byte{
		marshal,
		[]byte(subId),
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

// 查询农作物
func QueryPick(ctx *gin.Context)  {
	cropsId := ctx.Query("cropsId")
	resp, err := bc.ChannelExecute("queryPick", [][]byte{
		[]byte(cropsId),
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.String(http.StatusOK, bytes.NewBuffer(resp.Payload).String())
}